#include <pthread.h>
#include <stdio.h>

void *add(void *pv);

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int main(void) {
#define I (32750)
	pthread_t t[I];
	int n = 0;
	// pthread_mutexattr_t;
	// pthread_mutex_init(&mutex);
	// pthread_mutex_getprioceiling();
	for (int i = 0; i < I; ++i) {
		pthread_create(&t[i], NULL, add, &n);
	}
	for (int i = 0; i < I; ++i) {
		pthread_join(t[i], NULL);
	}
	pthread_mutex_destroy(&mutex);
	printf("n = %d\n", n);
	return 0;
}
void *add(void *pv) {
	int *p = (int *)pv;
	pthread_mutex_lock(&mutex);
	// pthread_mutex_trylock(&mutex);
	*p += 1;
	pthread_mutex_unlock(&mutex);
	return NULL;
}